home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / recvfrom.c < prev    next >
C/C++ Source or Header  |  1994-04-26  |  2KB  |  85 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <sys/socket.h>
  22. #include <hurd.h>
  23. #include <hurd/fd.h>
  24. #include <hurd/socket.h>
  25.  
  26. /* Read N bytes into BUF through socket FD from peer
  27.    at address ADDR (which is ADDR_LEN bytes long).
  28.    Returns the number read or -1 for errors.  */
  29. int
  30. DEFUN(recvfrom, (fd, buf, n, flags, addr, addr_len),
  31.       int fd AND PTR buf AND size_t n AND int flags AND
  32.       struct sockaddr *addr AND size_t *addr_len)
  33. {
  34.   error_t err;
  35.   mach_port_t addrport;
  36.   char *bufp = buf;
  37.   unsigned int nread = n;
  38.   mach_port_t *ports;
  39.   unsigned int nports;
  40.   char *cdata = NULL;
  41.   unsigned int clen = 0;
  42.  
  43.   if (err = HURD_DPORT_USE (fd, __socket_recv (port, &addrport,
  44.                            flags, bufp, &nread,
  45.                            &ports, &nports,
  46.                            &cdata, &clen,
  47.                            &flags,
  48.                            n)))
  49.     return __hurd_dfail (fd, err);
  50.  
  51.   /* Get address data for the returned address port.  */
  52.   {
  53.     char *buf = (char *) addr;
  54.     unsigned int buflen = *addr_len;
  55.     int type;
  56.  
  57.     err = __socket_whatis_address (addrport, &type, &buf, &buflen);
  58.     __mach_port_deallocate (__mach_task_self (), addrport);
  59.     if (err)
  60.       return __hurd_dfail (fd, err);
  61.  
  62.     if (buf != (char *) addr)
  63.       {
  64.     if (*addr_len < buflen)
  65.       *addr_len = buflen;
  66.     memcpy (addr, buf, *addr_len);
  67.     __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
  68.       }
  69.  
  70.     addr->sa_family = type;
  71.   }
  72.  
  73.   /* Toss control data; we don't care.  */
  74.   __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
  75.  
  76.   if (bufp != buf)
  77.     {
  78.       memcpy (buf, bufp, nread);
  79.       __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread);
  80.     }
  81.  
  82.   return nread;
  83. }
  84.  
  85.